﻿using System.Windows.Controls;
using System.Windows;
using DeepEarth.Toolkit;
using DeepEarth.Toolkit.Geometry.Data;
using System.Windows.Data;
using DeepEarth.Toolkit.Geometry;
namespace DeepEarth.Toolkit.Controls
{
    [TemplatePart(Name = PART_LabelText, Type = typeof(TextBox))]
    public class DigitizerToolPanel : Control
    {
        const string PART_LabelText = "PART_LabelText";

        TextBox labelText;

        public DigitizerToolPanel()
        {
            DefaultStyleKey = typeof(DigitizerToolPanel);            
        }

        public override void OnApplyTemplate()
        {
            labelText = (TextBox)GetTemplateChild(PART_LabelText);

            base.OnApplyTemplate();
        }

        #region properties
        public static readonly DependencyProperty ActiveGeometryProperty = DependencyProperty.Register("ActiveGeometry", typeof(Geometry.Data.Geometry), typeof(DigitizerToolPanel), new PropertyMetadata(ActiveGeometryChanged));
        public Geometry.Data.Geometry ActiveGeometry
        {
            get { return (Geometry.Data.Geometry)GetValue(ActiveGeometryProperty); }
            set { SetValue(ActiveGeometryProperty, value); }
        }

        static void ActiveGeometryChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            DigitizerToolPanel self = (DigitizerToolPanel)sender;
            Geometry.Data.Geometry newGeometry = (Geometry.Data.Geometry)args.NewValue;

            LabelGeometry labelGeo = newGeometry as LabelGeometry;

            if (labelGeo != null)
            {
                self.ActiveLabelGeometry = labelGeo;

                if (string.IsNullOrEmpty(labelGeo.Text))
                {
                    labelGeo.Text = Label.EmptyText;
                }

                if (self.labelText != null)
                {
                    self.labelText.Focus();
                    self.labelText.SelectAll();
                }
            }
            else
            {
                self.ActiveLabelGeometry = null;
            }
        }

        public static readonly DependencyProperty ActiveLabelGeometryProperty = DependencyProperty.Register("ActiveLabelGeometry", typeof(LabelGeometry), typeof(DigitizerToolPanel), null);
        public LabelGeometry ActiveLabelGeometry
        {
            get { return (LabelGeometry)GetValue(ActiveLabelGeometryProperty); }
            set { SetValue(ActiveLabelGeometryProperty, value); }
        }
      
        #endregion



    }
}
